package connect;

import java.sql.*;
import javax.swing.JOptionPane;

public class ConnectDB {

    public Connection conn;

    // Establish database connection
    public void getConnect() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url = "jdbc:sqlserver://Localhost:1433;database=IMPRO";
            conn = DriverManager.getConnection(url, "sa", "");
            System.out.println("Connected!");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error: " + e.getMessage(), "Conntion failed", JOptionPane.ERROR_MESSAGE);
        }
    }

    // Close connection
    public void closeConnect() {
        if (conn != null) {
            try {
                conn.close();
                conn = null;
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Error: " + e.getMessage(), "Close conntion fail", JOptionPane.ERROR_MESSAGE);
            }
            System.out.println("Disconnected!");
        }
    }
    
    // Test connect
    public static void main(String[] args) {
        ConnectDB con = new ConnectDB();
        con.getConnect();
        con.closeConnect();
    }
}
